import CONSTANTS from 'configs/constants'
import jwt, {JwtPayload} from 'jsonwebtoken'

// 生成token
function sign(data: any) {
    return jwt.sign(data, CONSTANTS.JWT_SECRET, {expiresIn: CONSTANTS.JWT_EXPIRE})
}  

// 验证token
function signVerify(token: string) {
    try {
        const decoded = jwt.verify(token, CONSTANTS.JWT_SECRET, {complete: true}) as JwtPayload;
        return {
            decoded,
            error: null
        }
    } catch(err) {
        return {
            decoded: null,
            error: err
        }
    }
}

export {
    sign,
    signVerify
}